package com.rc.saas.tenant.mq;

import com.alibaba.fastjson.JSON;
import com.rc.saas.tenant.dto.TransPayWalletResultDto;
import com.rc.saas.tenant.service.weixin.WxPayWalletService;
import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.weixin4j.pay.model.paywallet.TransPayWalletInfo;

/**
 * 对退款失败的付款单重新发起支付
 * Created by Sven on 2021/08/13
 */
@Component("transferOrderFailListener")
public class TransferOrderFailListener implements ChannelAwareMessageListener {
    private static Logger logger = LogManager.getLogger(TransferOrderFailListener.class);

    @Autowired
    private WxPayWalletService wxPayWalletService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        logger.info("transferOrderFailListener 收到了:{}", msg);

        try {
            TransPayWalletResultDto transPayWalletResultDto = JSON.parseObject(msg, TransPayWalletResultDto.class);
            TransPayWalletInfo transPayWalletInfo = wxPayWalletService.retryTransPayWallet(transPayWalletResultDto);

            logger.info(transPayWalletResultDto.getTenantCode() + ":对退款失败的付款单重新发起支付:{}", transPayWalletInfo.getPartner_trade_no());
        } catch (Exception e) {
            logger.error("transferOrderFailListener 收到了异常的JSON", e);
        }
    }
}
